package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mapper.ShopMapper;
import com.example.pojo.Shop;
import com.example.service.ShopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements ShopService {

    private final ShopMapper shopMapper;
    @Override
    public List<Shop> getAllShops() {
        LambdaQueryWrapper<Shop> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Shop::getPcode, 0);
        return shopMapper.selectList(lqw);
    }

    @Override
    public Boolean saveShop(Shop shop) {
        LambdaQueryWrapper<Shop> lqw = new LambdaQueryWrapper<>();
        //确保是用equals比
        lqw.eq(Shop::getPcode, "0");
        List<Shop> shops = shopMapper.selectList(lqw);
        for(Shop shop1: shops) {
            if (shop1.getCode().equals(shop.getCode())) {
                return false;
            }
        }
        shopMapper.insert(shop);
        return true;
    }

    @Override
    public List<Shop> getShop(String name, String code) {
        LambdaQueryWrapper<Shop> lqw = new LambdaQueryWrapper<>();
        lqw.eq(name != null && !name.isEmpty(), Shop::getName, name)
           .or()
           .eq(code != null && !code.isEmpty(), Shop::getCode, code);
        return shopMapper.selectList(lqw);
    }
}
